import domainUrl from '../config/config.js'
const request = {}

request.MyRequest = (url, data = {}, method = 'get') => {
	console.log(domainUrl + url)
	const token = uni.getStorageSync('token') || '';
	uni.hideLoading();
	uni.showLoading({
		title: "加载中",
		icon: "Loading",
		mask: "true"
	})
	var id = setTimeout(() => {
		uni.hideLoading();
		uni.showModal({
			title: "提示",
			content: "当前访问人数较多，请稍后重试",
			success: () => {
				uni.navigateBack();
				// console.log(that.urll)
			}
		})
	}, 7000)


	// if(token == ''){
	// 	uni.showToast({
	// 		title:'登录失效，请重新登录后访问',
	// 		icon:'none',
	// 		duration:'1000',
	// 		success: function() {

	// 			resolve('123')
	// 		}
	// 	});
	// }

	return uni.request({
		url: domainUrl + url,
		method: method,
		header: {
			'Authorization': token,
		},
		data: data,
		dataType: 'json',
	}).then(res => {
		clearTimeout(id)
		uni.hideLoading();
		console.log(res)
		if (res[1].data.status == 401) {
			
			uni.clearStorageSync()
			uni.removeStorageSync('nickName');
			uni.removeStorageSync('token');
			uni.removeStorageSync('phone');
			uni.showModal({
				title: '提示',
				content:'登录失效，请重新登录后访问',
				success: function(res) {
					console.log('登录失败！！！')
					uni.reLaunch({
						url:"/pages/login/login"
					})
				}
			});
		} else {
			return res[1]
		}

	}).catch(e => {
		console.log(e)
		throw e
	})
}

request.MyUpload = (url, name, filePath = '{}', method = 'get') => {
	const token = uni.getStorageSync('token') || '';
	var id = setTimeout(() => {
		uni.hideLoading();
		uni.showModal({
			title: "提示",
			content: "当前访问人数较多，请稍后重试",
			success: () => {
				uni.navigateBack();
				console.log(that.urll)
			}
		})
	}, 7000)
	return uni.uploadFile({
		url: domainUrl + url,
		method: method,
		header: {
			'Authorization': token,
		},
		name: name,
		filePath: filePath,
		dataType: 'json',
	}).then(res => {
		clearTimeout(id)
		return res[1]
	}).catch(e => {
		console.log(e)
	})
}

export default request
